package cym;

public class BubbleSort {

    public static void sort(int[] r) {
        int n = r.length;
        int temp;//辅助空间
        boolean flag = true;//是否交换的标识
        //最多n-1趟排序
        for (int i = 1; i < n && flag; i++) {
            flag = false;
            for (int j = 0; j < (n - i); j++) {
                if (r[j + 1] < r[j]) {//逆序则交换
                    temp = r[j];
                    r[j] = r[j + 1];
                    r[j + 1] = temp;
                    flag = true;
                }
            }
        }
    }

    public static void display(int[] r) {
        for (int item : r) {
            System.out.print(item + " ");
        }
    }

    public static void main(String[] args) {
        int[] r = new int[]{3, 5, 23, 4, 120};
        BubbleSort.sort(r);
        BubbleSort.display(r);
    }
}